//
//  space_view.m
//  mainpage
//
//  Created by Anthony Zhang on 2018/7/23.
//  Copyright © 2018年 John Zheng. All rights reserved.
//

#import "space_view.h"
#import "MJRefresh.h"
#import "content.h"
#import "contentModel.h"
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size

@interface space_view ()<UIScrollViewDelegate>
//用户名
@property (nonatomic,strong) NSMutableArray* user_name;
//用户头像
@property (nonatomic,strong) NSMutableArray* user_headview;
//用户文章
@property (nonatomic,strong) NSMutableArray* user_essay;
//scroll中用来计算view的位置
@property (nonatomic,assign) NSInteger* _pagenum;

@end

@implementation space_view

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //MARK:模拟从接口获取的数据
    _user_name = [[NSMutableArray alloc]initWithObjects:@"test_1",@"test_2",@"test_3",@"test_4", nil];
    _user_headview = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4", nil];
    _user_essay = [[NSMutableArray alloc]initWithObjects:@"帝高阳之苗裔兮，朕皇考曰伯庸。摄提贞于孟陬兮，惟庚寅吾以降。皇览揆余初度兮，肇锡余以嘉名：名余曰正则兮，字余曰灵均。纷吾既有此内美兮，又重之以修能。扈江离与辟芷兮，纫秋兰以为佩。汩余若将不及兮，恐年岁之不吾与。朝搴阰之木兰兮，夕揽洲之宿莽。日月忽其不淹兮，春与秋其代序。", nil];
    
    
    //MARK:设置单个view数据
    content* con_1 = [[content alloc]init];
    con_1.model = [contentModel modelWithName:_user_name[0] headview:_user_headview[0] essay:_user_essay[0]];
    con_1.frame = CGRectMake(0, 10, SCREEN_SIZE.width-20, SCREEN_SIZE.height/2-20);
    
    
    self.title = @"模拟空间";
    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backtomain)];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor grayColor];
    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, SCREEN_SIZE.width-20, SCREEN_SIZE.height)];
    scroll.delegate = self;
    scroll.contentSize = CGSizeMake(SCREEN_SIZE.width-20, SCREEN_SIZE.height);
    scroll.userInteractionEnabled = YES;
    scroll.showsVerticalScrollIndicator = NO;
    
    scroll.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headRefresh)];
    scroll.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];

    [scroll addSubview:con_1];
    
    
    [self.view addSubview:scroll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

//MARK:返回主页面
- (void)backtomain {
    [self.navigationController popViewControllerAnimated:YES];
}

//MARK:下拉刷新
- (void)headRefresh {
    
    [scroll.mj_header endRefreshing];
}

//MARK:上滑刷新
- (void)footerRefresh {
    
    pagenum++;
    
    content* con_new = [[content alloc]init];
    int i = arc4random_uniform(4);
    con_new.model = [contentModel modelWithName:_user_name[i] headview:_user_headview[i] essay:_user_essay[0]];
    con_new.frame = CGRectMake(0, (SCREEN_SIZE.height/2)*pagenum+20, SCREEN_SIZE.width-20, SCREEN_SIZE.height/2-20);
    [scroll addSubview:con_new];
    scroll.contentSize = CGSizeMake(SCREEN_SIZE.width-20, SCREEN_SIZE.height*(pagenum/2+1));
    [scroll reloadInputViews];
    [scroll.mj_footer endRefreshing];
    
}



@end
